package com.sidom.movieIndexer;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sidom.movieIndexer.domain.Movie;
import com.sidom.movieIndexer.facade.MovieIndexerFacade;

/**
 * Command line access point to the application
 * The location is the only argument that can be provided
 * @author Siebe
 */
public class MovieIndexer {
	private static Log log = LogFactory.getLog(MovieIndexer.class);
	
	/**
	 * Entry point of the application
	 * 
	 * @param args The location is the only argument that can be provided
	 */
	public static void main(String args[]){
		log.trace("Initializing Spring application context");
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
		MovieIndexerFacade movieIndexer = (MovieIndexerFacade)applicationContext.getBean("movieIndexer");
		log.trace("Initializing log4j");
		PropertyConfigurator.configure("src/main/resources/log4j.properties");
		log.trace("Starting application");
		CommandLineParser parser = new PosixParser();
		Options options = new Options();
		CommandLine line = null;
		try {
			line = parser.parse( options, args);
		}
		catch( ParseException exp ) {
		    log.error( "Input not correct. Define the path.", exp);
		}
		if(!CollectionUtils.isEmpty(line.getArgList())){
			List<Movie> movies = movieIndexer.indexMovies((String)line.getArgList().get(0));
			for (Movie movie : movies) {
				log.info(movie);
			}
		}
	}
}
